package com.example.dingdong.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.example.dingdong.daomain.MenuEntity;
import com.example.dingdong.repository.MenuRepository;
import com.example.dingdong.util.MyException;

/**
 * 
 * @ClassName: MenuService
 * @Description: 
 * @Author :chengwenixng
 * @DateTime 2022-03-24 22:42:34
 */
@Service
public class MenuService {
	
	private static final Logger log = LoggerFactory.getLogger(MenuService.class);

	@Autowired
	private MenuRepository menuRepository;
	
	/**
	 * 
	 * @Title: allMenuInformationData
	 * @Description: 
	 * @Author coding
	 * @DateTime 2022-03-24 22:42:29
	 * @return
	 */
	public List<MenuEntity> allMenuInformationData() {
		//获取去用户信息
		Subject currentUser = SecurityUtils.getSubject();
		if (currentUser.isAuthenticated()) {
			log.info("用户已经登录!");
		}
		List<MenuEntity> menuList  = new ArrayList<>();
		try {
			/**
			 * 查找数据
			 */
			menuList  = menuRepository.findByDelFlag(true);
		} catch (Exception e) {
			return new ArrayList<>();
		}
		return menuList ;
	}
	/**
	 * 
	 * @Title: getfindByMenuName
	 * @Description: 
	 * @Author coding
	 * @DateTime 2022-03-24 22:42:22
	 * @param menuName
	 * @param delflag
	 * @return
	 */
	public MenuEntity getfindByMenuName(String menuName, boolean delflag) {
		return menuRepository.findByMenuNameAndDelFlag( menuName, true);
	}
	/**
	 * 
	 * @Title: savaMenuEntities
	 * @Description: 
	 * @Author coding
	 * @DateTime 2022-03-24 22:44:40
	 * @param menuEntities
	 * @return
	 */
	public MenuEntity savaMenuEntities(MenuEntity menuEntities) {
		return menuRepository.save(menuEntities);
	}
	/**
	 * 
	 * @Title:  getMenuEntities
	 * @Description: 
	 * @Author coding
	 * @DateTime 2022-03-24 22:52:42
	 * @param id
	 * @return
	 */
	public MenuEntity getMenuEntities(Long id,Boolean deLFlag) {
		return menuRepository.findByIdAndDelFlag(id, true);
	}
	/**
	 * 
	 * @Title: getMenuEntity
	 * @Description: 
	 * @Author coding
	 * @DateTime 2022-03-24 23:04:46
	 * @param id
	 * @return
	 */
	public  MenuEntity getMenuEntity(Long id) {
		return  menuRepository.findByIdAndDelFlag(id, true);
	}
	/**
	 * 
	 * @Title: modifyMenuEntities
	 * @Description: 
	 * @Author coding
	 * @DateTime 2022-03-24 23:03:11
	 * @param request
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public void modifyMenuEntities(JSONObject request) throws Exception {
		Long id = request.getLong("id");
		String menuName =  request.getString("menuName");
		Double price =  request.getDouble("price");
		String dishDetails =  request.getString("dishDetails");
		Boolean dshclassification=  request.getBoolean("dshclassification");
		// 查询ID是否存在
		MenuEntity  menuEntity =getMenuEntity(id);
		if ( menuEntity == null) {
			throw new Exception("未找到相应的信息");
		}
		//保存数据
		menuEntity.setMenuName(menuName);
		menuEntity.setDishDetails(dishDetails);
		menuEntity.setPrice(price);
		menuEntity.setDshclassification(dshclassification);
		//保存数据
		menuRepository.save(menuEntity);
		
	}
	/**
	 * 
	 * @Title: getOption
	 * @Description: 
	 * @Author coding
	 * @DateTime 2022-03-25 00:51:58
	 * @param req
	 * @throws Exception
	 */
	public void getOption(JSONObject req ) throws Exception {
		/********** 1、校验数据 **********/
		Long id = req.getLong("id");
		int typeId = (int) req.get("typeId");
		// 查询ID是否存在
		MenuEntity  menuEntity =getMenuEntity(id);
		if ( menuEntity == null) {
			throw new Exception("未找到相应的信息");
		}
		/**
		 * 获取员工实体
		 */
		if(typeId == 0) {
			menuEntity.setIsSticky(!menuEntity.getIsSticky());
			menuRepository.save(menuEntity);
		}else if(typeId == 1){
			menuEntity.setIsRecommend(!menuEntity.getIsRecommend());
			menuRepository.save(menuEntity);
		}
	}
}
